<?php

class FakeController extends SpController
{
	public function setMap( $map )
	{
		$this->map = $map;
		return $this;
	}

	public function get( )
	{
		return sprintf( 'Hello world from %s( ). The name is "%s".', __METHOD__, $this->name( ) );
	}

	public function map( $name )
	{
		if( isset( $this->map ) && is_array( $this->map ) ) {
			return ( isset( $this->map[$name] ) ? $this->map[$name] : false );
		}
		return isset( $this->map ) ? $this->map : false;
	}
}

class SecondFakeController extends FakeController
{
	public function get( )
	{
		if( $this->name( ) == 'redirects' ) {
			return $this->redirect( '/' );
		}
		if( $this->name( ) === 'identity' ) {
			return $this->identity( );
		}
		return sprintf( 'Hello world from %s( ). The name is "%s".', __METHOD__, $this->name( ) );
	}

	public function getNew( )
	{
		return sprintf( 'Hello world from %s( ). The name is "%s".', __METHOD__, $this->name( ) );
	}

	public function getEdit( )
	{
		return sprintf( 'Hello world from %s( ). The name is "%s".', __METHOD__, $this->name( ) );
	}

	public function postEdit( )
	{
		return sprintf( 'Hello world from %s( ). The name is "%s".', __METHOD__, $this->name( ) );
	}

	public function postDelete( )
	{
		return sprintf( 'Hello world from %s( ). The name is "%s".', __METHOD__, $this->name( ) );
	}
}

class FakePreExecutingController extends FakeController {
	protected function preExecution( ) {
		// for the sake of simplicity, return a redirecting response
		return new SpHttpResponse( sprintf( 'Hello world from %s( )', __METHOD__ ) );
	}
}

class FakeViewUsingController extends FakeController
{
	public function get( )
	{
		return $this->render( 'foo.html', array( ) );
	}

	public function getNew( )
	{
		$this->test = 'test';
		return $this->render( 'foo.html', array( 'bla' => 'foo', 'test' => 'bar' ) );
	}

	public function getEdit( )
	{
		return $this->render( 'foo', array( ) );
	}
}

class FakeView extends SpPhpRenderer
{
	public function render( $filename, array $parameters = array( ) )
	{
		return sprintf( 'Rendering %s with parameters: %s', $filename, implode( ', ', array_keys( $parameters ) ) );
	}
}

class FakeBootstrap extends SpBootstrap
{
	protected $fakePath, $method;
	public function __construct( $path, $method, SpHttpRequest $request = null )
	{
		$this->fakePath = $path;
		$this->method = $method;
		$this->request = ( $request ) ? $request : new SpHttpRequest( );
		$this->setCreator( new FakeCreator( ) );
		$this->view = new FakeView;
	}

	public function path( )
	{
		return $this->fakePath;
	}

	public function method( )
	{
		return $this->method;
	}
}

class ExposedBootstrap extends SpBootstrap
{
	public function getCreator( )
	{
		return $this->creator( );
	}

	public function setRequest( $request )
	{
		$this->request = $request;
	}
}

class FakeRequest extends SpHttpRequest
{
	protected $query;
	public function __construct( Array $query, $host = 'example.com', $fullpath = '/foo.html', $file = 'index.php' )
	{
		parent::__construct( );
		$this->globals['query'] = $query;
		$this->globals['server'] = $this->globals['server'] + array(
			'HTTP_HOST' => $host,
			'REQUEST_URI' => $fullpath,
			'SCRIPT_FILENAME' => $file
		);
	}
	public function method( )
	{
		return 'GET';
	}
}

class FakeCreator implements SpCanCreateController
{
	protected $map;

	public function __construct( $map = array( ) )
	{
		$this->map = $map;
	}

	public function create( $classname )
	{
		if( isset( $this->map[$classname] ) ) {
			return $this->map[$classname];
		}
		return new $classname;
	}
}